package com.youkeda.dewu.service.impl;

import com.youkeda.dewu.dao.ProductDetailDAO;
import com.youkeda.dewu.dataobject.ProductDetailDO;
import com.youkeda.dewu.model.ProductDetail;
import com.youkeda.dewu.service.ProductDetailService;
import com.youkeda.dewu.util.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName ProductDetailServiceImpl.java
 * @Description TODO
 * @createTime 2020年10月08日 16:17:00
 */
@Service
public class ProductDetailServiceImpl implements ProductDetailService {
    @Autowired
    private ProductDetailDAO productDetailDAO;
    @Override
    public int save(ProductDetail productDetail) {

        ProductDetailDO productDetailDO = new ProductDetailDO(productDetail);
        if (StringUtils.isEmpty(productDetail.getId())){

            productDetailDO.setId(UUIDUtils.getUUID());
            return productDetailDAO.insert(productDetailDO);
        }
        return productDetailDAO.updateByPrimaryKey(productDetailDO);
    }

    @Override
    public List<ProductDetail> getByProductId(String productId) {
        if (StringUtils.isEmpty(productId)){
            return null;
        }
        List<ProductDetailDO> productDetailDOS = productDetailDAO.selectByProductId(productId);
        if (CollectionUtils.isEmpty(productDetailDOS)){
            return null;
        }
       List<ProductDetail> productDetails = productDetailDOS.stream().map(productDetailDO -> productDetailDO.toModel()).collect(Collectors.toList());

        return productDetails;
    }

    @Override
    public ProductDetail getById(String id) {
        if (StringUtils.isEmpty(id)){
            return null;
        }
        ProductDetailDO productDetailDO = productDetailDAO.selectByPrimaryKey(id);

        return productDetailDO.toModel();
    }

    @Override
    public List<ProductDetail> queryProductDetail(List<String> productDetailIds) {
        if (productDetailIds ==null){
            return null;
        }
        List<ProductDetailDO> productDetailDOS = productDetailDAO.selectByProductDetailIds(productDetailIds);
        List<ProductDetail> productDetails = productDetailDOS.stream().map(productDetailDO -> productDetailDO.toModel()).collect(Collectors.toList());

        return productDetails;
    }
}
